<template>
  <div>
    <el-form :inline="true" :model="queryParams"  size="mini">
      <el-form-item :label="$t('name')">
        <el-input v-model="queryParams.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="requestData"  icon="el-icon-search">{{ $t('search') }}</el-button>
        <el-button type="primary" v-if="addPermission"  @click="handleAddShow" icon="el-icon-plus">{{ $t('add') }}</el-button>
      </el-form-item>
    </el-form>

    <el-table
            :data="tableListData"
            v-loading="loading"
            row-key="id"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
            border stripe
            class="init_table">
      <el-table-column
              :label="$t('name')"
              min-width="200"
              prop="name"
              show-overflow-tooltip
              align="left">
      </el-table-column>
      <el-table-column
              prop="is_show_text"
              :label="$t('is_show')">
      </el-table-column>
      <el-table-column
              prop="status_text"
              :label="$t('status')">
      </el-table-column>
        <el-table-column
            prop="is_admin_text"
            :label="$t('is_admin')">
        </el-table-column>
        <el-table-column
            :label="$t('sequence')"
            prop="sequence">
        </el-table-column>

      <el-table-column
              align="center"
              :label="$t('actions')">
        <template slot-scope="scope">
          <el-button
                  v-if="updatePermission"
                  size="mini"
                  @click="handleEditShow(scope.$index, scope.row)">{{ $t('edit') }}</el-button>
          <el-button
                  v-if="deletePermission"
                  type="danger"
                  size="mini"
                  @click="handleDelete(scope.$index, scope.row)">{{ $t('delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="$t('add')" :visible.sync="dialogAddFormVisible" width="40%">
      <el-form :model="addForm" ref="addForm">
        <el-form-item :label="$t('name')" prop="name" :label-width="formLabelWidth">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
          <el-form-item :label="$t('parent')" prop="pid" :label-width="formLabelWidth">
              <m-category-cascader :refresh="refresh" :c-id.sync="addForm.pid"></m-category-cascader>
          </el-form-item>
        <el-form-item :label="$t('is_show')" prop="isShow" :label-width="formLabelWidth">
          <el-radio v-model="addForm.is_show" label="1">是</el-radio>
          <el-radio v-model="addForm.is_show" label="0">否</el-radio>
        </el-form-item>
        <el-form-item :label="$t('status')" prop="status" :label-width="formLabelWidth">
          <el-radio v-model="addForm.status" label="1">启用</el-radio>
          <el-radio v-model="addForm.status" label="0">禁用</el-radio>
        </el-form-item>
          <el-form-item :label="$t('sequence')" prop="sequence" :label-width="formLabelWidth">
              <el-input v-model.number="addForm.sequence"></el-input>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAddFormVisible = false">{{ $t('cancel') }}</el-button>
        <el-button type="primary" @click="handleAdd">{{ $t('confirm') }}</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="$t('edit')" :visible.sync="dialogEditFormVisible" width="40%">
      <el-form :model="editForm" ref="editForm">
        <el-form-item :label="$t('name')" prop="name" :label-width="formLabelWidth">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
          <el-form-item :label="$t('parent')" prop="pid" :label-width="formLabelWidth">
              <m-category-cascader :edit-refresh="refresh" :c-id.sync="editForm.pid"></m-category-cascader>
          </el-form-item>
          <el-form-item :label="$t('is_show')" prop="isShow" :label-width="formLabelWidth">
              <el-radio v-model="editForm.is_show" label="1">是</el-radio>
              <el-radio v-model="editForm.is_show" label="0">否</el-radio>
          </el-form-item>
          <el-form-item :label="$t('status')" prop="status" :label-width="formLabelWidth">
              <el-radio v-model="editForm.status" label="1">启用</el-radio>
              <el-radio v-model="editForm.status" label="0">禁用</el-radio>
          </el-form-item>
          <el-form-item :label="$t('sequence')" prop="sequence" :label-width="formLabelWidth">
              <el-input v-model.number="editForm.sequence"></el-input>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">{{ $t('cancel') }}</el-button>
        <el-button type="primary" @click="handleEdit">{{ $t('confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {add, getMCategoryList, edit, del} from '../../../api/mCategory'
import {addSuccess, deleteSuccess, editSuccess, tableDefaultData} from '../../../libs/tableDataHandle'
import {hasPermission} from '../../../libs/permission'
import MCategoryCascader from '../../../components/Cascader/MCategory'
import notify from "../../../libs/notify";

export default {
    name: 'mCategoryIndex',
      components:{
          MCategoryCascader,
      },
    data: () => ({
      ...tableDefaultData(),
      tableListData: [],
      foldList: [],
      addForm: {
        name:'',
        is_show:"0",
        status:"1",
          pid:0,
          sequence:0,
      },
      editForm: {
          id:'',
          name:'',
          is_show:'',
          status:'',
          pid:'',
          sequence:'',
      },
        refresh:"",
    }),
    methods: {
        handleAddShow() {
            this.dialogAddFormVisible = true
            this.refresh = Math.random() + ''
        },
        handleEditShow(index, row) {
            this.editForm.id = row.id
            this.editForm.name = row.name
            this.editForm.is_show = row.is_show + ''
            this.editForm.status = row.status + ''
            this.editForm.pid = row.parent_id
            this.editForm.sequence = row.sequence
            this.refresh = Math.random() + ''
            this.dialogEditFormVisible = true
        },
      handleAdd () {
        add(this.addForm).then( response => {
          addSuccess(this)
            this.addForm.pid = 0
          this.requestData()
        })
      },
        handleEdit() {
            edit(this.editForm).then(response => {
                editSuccess(this)
                this.requestData()
            })
        },
        handleDelete(index, row) {
            del({id:row.id}).then(response => {
                notify.success('删除成功', this)
                this.requestData()
            })
        },
      requestData () {
        this.loading = true
          getMCategoryList(this.queryParams).then(response => {
              this.tableListData = response.data.data
              this.options = response.data.data
              this.loading = false
          })
      },
    },
    computed: {
      updatePermission: function() {
        return hasPermission('m-category.edit')
      },
      addPermission: function() {
        return hasPermission('m-category.add')
      },
      deletePermission: function() {
        return hasPermission('m-category.del')
      }
    },
    created() {
      this.requestData()
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
